﻿$(function() {
	var location = (window.location+'').split('/');  
	var basePath = location[0]+'//'+location[2]+'/'+location[3]+'/';  
	
	addEven();
	var OriginalImage=[];
	var m=2;
	var function_id='20150117';
	var i=$("#mothershow div[class^=kz-upload]").length;
	function loadpic(d,e,a){
		 $("#imageform").ajaxForm({   
//	            target: ('#mothershow'),
			 	dataType: "json",
	            beforeSubmit:function(){ 
	            	 $('#myModal').modal({
						 keyboard: true,
						 backdrop: 'static',
						 });
	            },  
	            success:function(data){ 
	            	$('#myModal').modal('hide');
	            	var jsondata=eval('('+data+')');
					$("#"+d).prev().prev().attr("src",jsondata[0].OriginalImage);   //调整页面
					$("#"+d).prev().html(jsondata[0].OriginalImage);
					$("#"+d).prev().css("display","none");
					$("#"+d).prev().attr("name","image_src");
					$("#"+d).parent().append("<p class='delete'></p>");
					$("#"+d).css("display","none");
					$("#"+d).siblings('P').on('click',function(){ //给删除图片绑定点击事件
						//默认只能上传5张图片，如果删除最后一张图片，需要添加一个上传框
						if($("#mothershow div[class^=kz-upload]").length==5 && document.getElementById("file5")!=null){
							$("#mothershow").append("<div class='kz-upload'>"
									+'<div>'
									+'<img width="100%" name="fjdz" height="100%" src="" alt="">'
									+'<p  name="1" style="text-align: center;font-size: 12px;margin-top: 0px;">添加图片</p>'
									+'<input name="file" type="file" id="file" multiple="multiple">'
									+'</div>'
									+'</div>'
									);
							addEven();   //递归，动态生成的dom节点需要从新绑定change
		}
						$(this).parent().parent().remove();
					});
					i=$("#mothershow div[class^=kz-upload]").length;
					if(i<4){
						$("#mothershow").append("<div class='kz-upload'>"
													+'<div>'
													+'<img name="fjdz" width="100%" height="100%" src="" alt="">'
													+'<p >添加图片</p>'
													+'<input name="file" type="file" id="file'+m+'" multiple="multiple">'
													+'</div>'
													+'</div>'
													);
						m+=2;
					}else if(i==4){
						$("#mothershow").append("<div class='kz-upload'>"
								+'<div>'
								+'<img width="100%" name="fjdz" height="100%" src="" alt="">'
								+'<p>添加图片</p>'
								+'<input name="file" type="file" id="file5" multiple="multiple">'
								+'</div>'
								+'</div>'
								);
					}
					if(i<5){
						addEven();
					}
	            },  
	            error:function(XMLHttpRequest, textStatus, errorThrown) {
	            	$('#myModal').modal('hide');
                    alert("图片过大或者不支持该图片类型");
                }
                }).submit(); 
	}
	function addEven(){
		$("input[name^='file']").change(function(e) {
			var a = getObjectURL(this.files[0]);  //获取文件路径
			if (a) {
				var d=[this.id];
				loadpic(d,function_id,a);
			}
		});
	}
	function getObjectURL(a) {
		var b = null;
		if (window.createObjectURL != undefined) {
			b = window.createObjectURL(a);
		} else if (window.URL != undefined) {
			b = window.URL.createObjectURL(a);
		} else if (window.webkitURL != undefined) {
			b = window.webkitURL.createObjectURL(a);
		}
		return b;
	}
	$(document).on('click', '.submit', function() {
		var e = {};
		var url;
		var spbh = $("#submit-xiug").attr("spbh");
		if(this.id==="submit_lost"){
			url="lostandfound/saveLostInfo";
		}else if(this.id==="submit_found"){
			url="lostandfound/saveFoundInfo";
		}else if(this.id==="submit"){
			url="tzsc/fbList.action";
		}else if(this.id==="submit-xiug"){
			url="tzsc/xgsp.action?spid="+spbh;
		}
		var d=$("img[name^='fjdz']");
		if(OriginalImage.length===0){
		for (var i = 0; i < d.length-1; i++) {
			OriginalImage.push(d[i].src);
		}
		if(d.length === 1){
			OriginalImage.push(d[0].src);
		}
		}else{
			OriginalImage=[];
			for (var i = 0; i < d.length-1; i++) {
				OriginalImage.push(d[i].src);}
			if(d.length === 1){
				OriginalImage.push(d[0].src);
			}
		}
		e["spmc"] =$('input[name=spmc]').val();
		e["splx"] = $('select[name=splx]').val();
		e["spzt"] = $('select[name=spzt]').val();
		e["spjg"] = $("input[name=spjg]").val();
		e["spms"] = $("textarea[name=spms]").val();
		e["lxdh"] = $("input[name=lxdh]").val();
		e["pics"]=OriginalImage.join();
		if (e["spmc"].length == 0 || e["splx"].length == 0 || e["spzt"].length == 0 || e["spjg"].length ==0 || e["spms"].length==0 ||e["lxdh"].length==0 || e["pics"]==basePath) {
			alert("信息未完善！");
			return
		}
		$.ajax({
			url: url,
			secureuri: false,
			data: e,
			beforeSend:function(){
				 $('#myModal').modal({
					 keyboard: true,
					 backdrop: 'static',
					 });
			},
			success: function(a, b) {
				$('#myModal').modal('hide');
				var data=eval('('+a+')');
				$('.modal-body p').html(data.message);
				 $('#myModal1').modal({
					 keyboard: true,
					 backdrop: 'static',
				 });
//				alert(data.message);
				
	/*			location.href='lostandfound/getMyReleaseInformation?nowpage=1&pagesize=8';*/
//				$("a class^=[submit]").attr("disabled",true);
//				alert(JSON.parse(a)["message"]);
//				window.location.href = encodeURI("/MV/zhihui/mobile/zhihui/foundlist.html");
			},
			error: function(a, b) {
			}
		});
	});
});